import { downloadFile, queryUrlParams } from '@/utils/index'
import request from '@/utils/request'

// 查询药品清单
export function getDrugHousingList(drugName, drugType) {
  const data = {
    page: 0,
    size: 50,
    drugName,
    drugType
  }
  const param = queryUrlParams(data)
  return request({
    url: 'drugdelivery/getDrugHousingDetailList?' + param,
    method: 'get'
  })
}

// 查询领用人列表，暂时查询当前系统用户
export function getCollectUses() {
  return request({
    url: 'api/users?page=0&size=50&enabled=true',
    method: 'get'
  })
}

export function addDrugDelivery(data) {
  return request({
    url: 'drugdelivery/add',
    method: 'post',
    data
  })
}

export function getDetail(id) {
  return request({
    url: 'drugdelivery/detail?id=' + id,
    method: 'get'
  })
}

export function cancelDrugDelivery(id) {
  return request({
    url: 'drugdelivery/update?id=' + id,
    method: 'post'
  })
}

export function getDepartments() {
  return request({
    url: 'api/department/list',
    method: 'get'
  })
}

export function getValidityDrug() {
  const data = {
    page: 0,
    size: 40
  }
  const param = queryUrlParams(data)
  return request({
    url: 'drugdelivery/getValidityDrugDetailList?' + param,
    method: 'get'
  })
}

// 导出出库单(批量)
export function outTemplate() {
  request({
    url: 'drugdelivery/outTemplate',
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    downloadFile(result, '出库单', 'xlsx')
  })
}

// 导出出库单(单条出库记录)
export function outDelivery(data, backFun) {
  const param = queryUrlParams(data)
  request({
    url: 'drugdelivery/outDelivery?' + param,
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    downloadFile(result, '出库单', 'xlsx')
    backFun()
  })
}
